package com.dd.cloud.user.service.doctor.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.utils.ObjectUtils;
import com.dd.cloud.user.entity.doctor.DoctorDetail;
import com.dd.cloud.user.entity.doctor.DoctorSettingService;
import com.dd.cloud.user.mapper.doctor.DoctorSettingServiceMapper;
import com.dd.cloud.user.req.doctor.DoctorSettingServiceFrom;
import com.dd.cloud.user.service.doctor.IDoctorDetailService;
import com.dd.cloud.user.service.doctor.IDoctorSettingServiceService;
import com.gitee.sunchenbin.mybatis.actable.annotation.Column;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 * 医生设置服务 服务实现类
 * </p>
 *
 * @author czg
 * @since 2023-07-07
 */
@Service
public class DoctorSettingServiceServiceImpl extends ServiceImpl<DoctorSettingServiceMapper, DoctorSettingService>
        implements IDoctorSettingServiceService {

    @Autowired
    private IDoctorDetailService doctorDetailService;

    @Override
    public DoctorSettingService getDoctorSettingService(Integer doctorId) {
        LambdaQueryWrapper<DoctorSettingService> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DoctorSettingService::getDoctorId, doctorId);
        DoctorSettingService doctorSettingService = this.getOne(wrapper);
        //查询医生是否设置互联网资质
        Integer isTip = doctorDetailService.getIsPush(doctorId);
        if (doctorSettingService != null) {
            doctorSettingService.setIsTip(isTip);
        } else {
            doctorSettingService = new DoctorSettingService();
            doctorSettingService.setIsTip(isTip);
        }
        return doctorSettingService;
    }

    @Override
    public void addOrUpdateDoctorSettingService(DoctorSettingServiceFrom doctorSettingServiceFrom) {
        LambdaQueryWrapper<DoctorSettingService> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DoctorSettingService::getDoctorId, doctorSettingServiceFrom.getDoctorId());
        DoctorSettingService doctorSettingService = this.getOne(wrapper);
        Integer isCheckA = doctorSettingServiceFrom.getIsOpenInquiry();
        Integer isCheckB = doctorSettingServiceFrom.getIsOpenConsult();
        if (isCheckA == 1 && ObjectUtil.isNull(doctorSettingServiceFrom.getInquiryPrice())) {
            throw new ZekeException("问诊价格不能为空");
        }
        if (isCheckB == 1 && ObjectUtil.isNull(doctorSettingServiceFrom.getConsultPrice())) {
            throw new ZekeException("咨询价格不能为空");
        }
        if (doctorSettingService == null) {
            DoctorSettingService newDoctorSettingService = new DoctorSettingService();
            BeanUtils.copyProperties(doctorSettingServiceFrom, newDoctorSettingService);
            newDoctorSettingService.setCreateDate(LocalDateTime.now())
                    .setUpdateDate(LocalDateTime.now());
            boolean flag = this.save(newDoctorSettingService);
            if (!flag) {
                throw new ZekeException("保存失败");
            }
        } else {
            LambdaUpdateWrapper<DoctorSettingService> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(DoctorSettingService::getDoctorId, doctorSettingServiceFrom.getDoctorId())
                    .set(DoctorSettingService::getDeptName, doctorSettingServiceFrom.getDeptName())
                    .set(DoctorSettingService::getConsultPrice, doctorSettingServiceFrom.getConsultPrice())
                    .set(DoctorSettingService::getIsOpenConsult, doctorSettingServiceFrom.getIsOpenConsult())
                    .set(DoctorSettingService::getInquiryPrice, doctorSettingServiceFrom.getInquiryPrice())
                    .set(DoctorSettingService::getIsOpenRecommend, doctorSettingServiceFrom.getIsOpenRecommend())
                    .set(DoctorSettingService::getIsOpenInquiry,doctorSettingServiceFrom.getIsOpenInquiry())
                    .set(DoctorSettingService::getUpdateDate, LocalDateTime.now());
            boolean flag = this.update(updateWrapper);
            if (!flag) {
                throw new ZekeException("更新失败");
            }
        }
    }
}
